<?php
declare (strict_types=1);

namespace app\api\model\v1;

use think\Model;

/**
 * @mixin \think\Model
 */
class Template extends AppModel
{
    //


    //一级类别
    public function templateType()
    {
        return $this->hasMany(TemplateType::class, 'template_id');
    }

    //二级类别
    public function templateSecondLevel()
    {
        return $this->hasMany(TemplateSecondLevel::class, 'template_id');
    }

    //详细属性
    public function templateDetail()
    {
        return $this->hasMany(TemplateDetail::class, 'template_id');
    }


    /**
     * @param $arrData
     * @param $template_id
     * @param $year
     * @param $create_id
     * @User: 刘海龙
     * @Date: 2021/9/24
     * @Time: 14:32
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 处理数据
     */
    public static function saveExcel($arrData, $template_id, $year, $create_id)
    {
        if (!empty($arrData)) {
            $res = self::find($template_id);
            if (is_null($res)) {
                $result = 403;
            } else {
                foreach ($arrData as $key => $value) {
                    $template_type_id = TemplateType::create([
                        'template_type_title' => $value['template_type_title'] ?? '',
                        'year' => $year ?? '',
                        'template_id' => $template_id ?? 0,
                        'create_id' => $create_id ?? 0,
                    ])->id;
                    if (isset($value['item'])) { //判断是否包含item字段
                        //如果包含item字段 循环
                        foreach ($value['item'] as $child_key => $child_value) {
                            $template_second_level_id = TemplateSecondLevel::create([
                                'second_level_title' => $child_value['second_level_title'] ?? '',
                                'year' => $year ?? '',
                                'template_id' => $template_id ?? 0,
                                'template_type_id' => $template_type_id ?? 0,
                                'create_id' => $create_id ?? 0,
                            ])->id;
                            if (isset($child_value['item'])) {
                                foreach ($child_value['item'] as $child_child_key => $child_child_value) {
                                    TemplateDetail::create([
                                        'template_detail_title' => $child_child_value['template_detail_title'] ?? '',
                                        'year' => $year ?? '',
                                        'desc' => $child_child_value['desc'] ?? '',
                                        'deduct_marks_standard' => $child_child_value['deduct_marks_standard'] ?? '',
                                        'once_deduct_marks' => $child_child_value['once_deduct_marks'] ?? '',
                                        'most_deduct_marks' => $child_child_value['most_deduct_marks'] ?? '',
                                        'template_id' => $template_id ?? 0,
                                        'template_type_id' => $template_type_id ?? 0,
                                        'template_second_level_id' => $template_second_level_id ?? 0,
                                        'create_id' => $create_id ?? 0,
                                    ]);
                                }
                            }
                        }
                    }
                }
                $result = 200;
            }
        } else {
            $result = 500;
        }

        return $result;
    }


    /**
     * @param $id
     * @param $num //定义随机抽取的数量
     * @User: 刘海龙
     * @Date: 2021/9/27
     * @Time: 17:18
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 随机生成
     */
    public static function randomTemplate($id, $num,$type)
    {
        $res = Template::find($id);
        if (is_null($res)) {
            return [
                'code' => 403,
                'msg' => '无法找到模版问卷',
                'data' => []
            ];
        }
        //根据问卷id找到一级大类
        $template_type = TemplateType::with(['templateSecondLevel' => ['templateDetail']])->where(['template_id' => $id])->select()->toArray();
        if ($type === 'random'){
            if ($num <= 1) {
                return [
                    'code' => 403,
                    'msg' => '随机抽取数量必须大于1',
                    'data' => []
                ];
            } else if ($num <= count($template_type)) {
                $rand = array_rand($template_type, $num);
                $new_arr = [];
                for ($i = 0; $i < $num; $i++) {
                    $new_arr[] = $template_type[$rand[$i]];
                    if (count($template_type[$rand[$i]]['templateSecondLevel']) > $num) {
                        $abc = array_rand($template_type[$rand[$i]]['templateSecondLevel'], $num);
                        $abc_new_arr = [];
                        for ($j = 0; $j < count($abc); $j++) {
                            $abc_new_arr[] = $template_type[$rand[$i]]['templateSecondLevel'][$abc[$j]];
                        }
                        $new_arr[$i]['templateSecondLevel'] = $abc_new_arr;
                    } else {
                        $new_arr[$i]['templateSecondLevel'] = $template_type[$rand[$i]]['templateSecondLevel'];
                    }
                }
                return [
                    'code' => 200,
                    'msg' => '成功',
                    'data' => $new_arr
                ];
            } else {
                return [
                    'code' => 403,
                    'msg' => '随机抽取数量大于当前题库存在的数量',
                    'data' => []
                ];
            }
        }else{
            if (empty($template_type)) {
                return [
                    'code' => 403,
                    'msg' => '处理失败,暂无数据',
                    'data' => []
                ];
            }
            return [
                'code' => 200,
                'msg' => '成功',
                'data' => $template_type
            ];
        }

    }


//    /**
//     * @param $id
//     * @User: 刘海龙
//     * @Date: 2021/9/27
//     * @Time: 17:41
//     * @return array
//     * @throws \think\db\exception\DataNotFoundException
//     * @throws \think\db\exception\DbException
//     * @throws \think\db\exception\ModelNotFoundException
//     * 生成所有数据
//     */
//    public static function templateAllCreate($id)
//    {
//
//        $res = TemplateType::with(['templateSecondLevel' => ['templateDetail']])->where(['template_id' => $id])->select()->toArray();
//        if (empty($res)) {
//            return [
//                'code' => 403,
//                'msg' => '处理失败,暂无数据',
//                'data' => []
//            ];
//        }
//        return [
//            'code' => 200,
//            'msg' => '成功',
//            'data' => $res
//        ];
//    }

}
